package com.xjiuge.fruit.controller.manage;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xjiuge.fruit.bean.User;
import com.xjiuge.fruit.common.ComIdUtil;
import com.xjiuge.fruit.common.JsonResult;
import com.xjiuge.fruit.common.Md5Util;
import com.xjiuge.fruit.common.Response;
import com.xjiuge.fruit.common.ResponseCodeConstant;
import com.xjiuge.fruit.service.UserService;

@Controller("UserController")
@RequestMapping("/user")
public class UserController {

	public final static Logger LOG = Logger.getLogger(UserController.class);
	
	@Resource(name = "userService")
	private UserService userService;
	
	/**
	 * 查询所有用户
	 * UserController.selectAllUser<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年10月30日  上午10:45:18</p>
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectAllUser",method = RequestMethod.POST)
	public JsonResult selectAllUser(){
		Integer code = null;
		String msg = "";
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<User> list = userService.selectAllUser();
			code = ResponseCodeConstant.Common.success.getCode();
			msg = ResponseCodeConstant.Common.success.getValue();
			map.put("userList", list);
		} catch (Exception e) {
			code = ResponseCodeConstant.Common.error.getCode();
			msg = "查询所有用户异常"+e;
			LOG.error("查询所有用户异常"+e);
		}
		Response response = new Response();
		response.setCode(code);
		response.setMsg(msg);
		response.setData(map);
		return new JsonResult(response);
	}
	
	/**
	 * 添加用户
	 * UserController.addUser<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年10月31日  上午10:45:35</p>
	 * @param user
	 * @param httpSession
	 * @return
	 */
	@RequestMapping(value = "/addUser",method = RequestMethod.POST)
	@ResponseBody
	public JsonResult addUser(User user,HttpSession httpSession){
		Integer code = null;
		String msg = "";
		User loginUser = (User) httpSession.getAttribute("user");
		try {
			User user2 = userService.selectUserByLoginName(user.getLoginName());
			if(null != user2){
				code = ResponseCodeConstant.Common.Business_exception.getCode();
				msg = "用户名已存在";
			}else{
				if("".equals(user.getLoginName()) || null == user.getLoginName()){
					code = ResponseCodeConstant.Common.Business_exception.getCode();
					msg = "用户名不能为空";
				}else{
					String uuid = ComIdUtil.getUUID();
					user.setLoginPwd(Md5Util.getMd5(user.getLoginPwd()));
					user.setUserId(uuid);
					user.setCreateTime(new Date());
					user.setCreaterId(loginUser.getUserId());
					user.setCreaterName(loginUser.getUserName());
					userService.save(user);
					code = ResponseCodeConstant.Common.success.getCode();
					msg = ResponseCodeConstant.Common.success.getValue();
				}
			}
		} catch (Exception e) {
			code = ResponseCodeConstant.Common.error.getCode();
			msg = "添加用户异常"+e;
			LOG.error("添加用户用户异常"+e);
		}
		Response response = new Response();
		response.setCode(code);
		response.setMsg(msg);
		return new JsonResult(response);
	}
	
	/**
	 * 批量删除用户
	 * UserController.deleteUser<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年10月31日  上午10:47:43</p>
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/deleteUser",method = RequestMethod.POST)
	@ResponseBody
	public JsonResult deleteUser(String [] ids){
		Integer code = null;
		String msg = "";
		try {
			if(ids.length>0){
				userService.deleteByIds(ids);
				code = ResponseCodeConstant.Common.success.getCode();
				msg = ResponseCodeConstant.Common.success.getValue();
			}else{
				code = ResponseCodeConstant.Common.Business_exception.getCode();
				msg = "用户ID不能为空！";
			}
		} catch (Exception e) {
			code = ResponseCodeConstant.Common.error.getCode();
			msg = "用户ID不能为空！"+e;
			LOG.error("用户ID不能为空！"+e);
		}
		Response response = new Response();
		response.setCode(code);
		response.setMsg(msg);
		return new JsonResult(response);
	}
	
	/**
	 * 编辑用户
	 * UserController.updateUser<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年10月31日  下午3:45:02</p>
	 * @return
	 */
	@RequestMapping(value = "/updateUser",method = RequestMethod.POST)
	@ResponseBody
	public JsonResult updateUser(User user){
		Integer code = null;
		String msg = "";
		try {
			if(user == null || user.getUserId() == null || user.getUserId().equals("")){
				code = ResponseCodeConstant.Common.Business_exception.getCode();
				msg = "用户ID不能为空！";
			}
			userService.update(user);
			code = ResponseCodeConstant.Common.success.getCode();
			msg = ResponseCodeConstant.Common.success.getValue();
		} catch (Exception e) {
			code = ResponseCodeConstant.Common.error.getCode();
			msg = "编辑用户异常"+e;
			LOG.error("编辑用户异常"+e);
		}
		Response response = new Response();
		response.setCode(code);
		response.setMsg(msg);
		return new JsonResult(response);
	}
}
